import { Page } from '@/base/Page';
import { deepAssign } from '@/base/util/Objects';
import {
  deleteCategory,
  deleteProduct, getProductAll,
  getProductCategoryList,
  getProductList, postCategory,
  putCategorySort,
  PutProductId
} from '@/base/api/LvApi';
import {ProductCategory} from '@/base/model/ProductCategory';
import {Product} from '@/base/model/Product';

export class ProductListBloc {
  currentIdx: number = 0;
  sortDevice = new ProductCategory();
  model: any = {
    id: 0 || undefined,
    // modelIcon: '',
    modelName: '',
    configure: '',
    sortId: 0,
    poster: '',
    createTime: '',
    updateTime: ''
  };
  modelPut: any = {
    id: 0 || undefined,
    companyId: 0 || undefined,
    isFirmware: true,
    isSelfMotion: true,
    isSoftware: true,
    modelIcon: '',
    modelName: '',
    configure: '',
    sortId: 0 || undefined,
    poster: '',
    createTime: '',
    updateTime: ''
  };
  sortPage: Page<ProductCategory> = new Page<ProductCategory>([], 0, 1, 10, 0, []);

  modelPage: Page<Product> = new Page<Product>([], 0, 1, 15, 0, []);

  loading = false;
  sortList: any = [];
  num: number = 0;
  fileList:any = [];
  fileList1:any = [];

  addSort(name, state, icon) {
    this.loading = true;
    return postCategory({
      sortName: name,
      sortState: state,
      sortIcon: icon
    }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  editSort(id: number, sortName: string, sortState, sortIcon) {
    this.loading = true;
    return postCategory({id, sortName, sortState, sortIcon}).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  deleteSort(id: number) {
    this.loading = true;
    return deleteCategory({ id }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  putCategorySort(id: []) {
    this.loading = true;
    return putCategorySort({ id }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }
  getProductList(pageIndex: number, pageSize: number, sortDirection: string, categoryId, modelName) {
    this.loading = true;
    return getProductList({
      pageIndex,
      pageSize,
      sortDirection,
      categoryId: categoryId,
      productName: modelName
    }).then((res) => {
      deepAssign(this.modelPage, res.data);
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  getAllCategory(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    let that = this;
    return getProductCategoryList({
      pageIndex,
      pageSize,
      sortDirection
    }).then((res) => {
      that.sortPage = res.data;
      this.sortList = res.data.items.map(function (item) {
        var obj = { id: 0, sortName: '', imgUrl: '', isAble: 1, sort: 0 };
        // @ts-ignore
        obj.id = item.productCategoryId;
        obj.sortName = item.categoryName;
        obj.imgUrl = item.imgUrl;
        obj.isAble = item.isAble;
        obj.sort = item.sort
        return obj;
      });
      return this.sortList;
    }).finally(() => {
      this.loading = false;
    });
  }
  getAllProduct() {
    let that = this;
    this.num = 0;
    return getProductAll({}).then((res) => {
      res.data.items.map(function (item) {
        if (item.isUp === 1) {
         that.num = that.num + 1;
        }
      });
      return this.num;
    }).finally(() => {
      this.loading = false;
    });
  }
  getsortName(sortId: number) {
    const item = this.sortList.find(x => x.id === sortId);
    return item == null ? '' : item.sortName;
  }
  switchStatus(id, value, index, sym) {
    if (sym && this.num === 4) {
      // 判断当前列表里有多少置顶的
      let error = '已选择四个置顶，请先取消！';
      return Promise.reject(error);
      // return false;
    } else {
      this.modifyItem(id, value, sym);
    }

  }
  modifyItem(
    productId: number,
    status: number,
    sym: number
  ) {
    this.loading = true;
    return PutProductId({
      productId,
      status,
      sym
    }).then((res) => {
      // deepAssign(this.form, res.data);
      this.getProductList(this.modelPage.pageIndex,
        this.modelPage.pageSize,
        'DESC',
        this.sortDevice.productCategoryId,
        this.model.modelName
      );
      // return res;
    }).finally(() => {
      this.loading = false;
    })
  }
  deleteProduct(id) {
    return deleteProduct({ id }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }
}
